<template>
  <div class="app-container">
    <div id="map"></div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="包间名称" prop="compartmentName">
        <el-input
          v-model="queryParams.compartmentName"
          placeholder="请输入包间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="床位数量" prop="bedNumber">
        <el-input
          type="number"
          v-model="queryParams.bedNumber"
          placeholder="请输入床位数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['business:booth:query']"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:booth:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:store:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:booth:del']"
          >批量删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:store:export']"
        >导出123</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="storeList"
      @selection-change="handleSelectionChange"
      max-height="650px"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="归属区域"
        align="center"
        :show-overflow-tooltip="true"
        prop="regionName"
      />

      <el-table-column
        label="归属门店"
        align="center"
        :show-overflow-tooltip="true"
        prop="storeName"
      />
      <el-table-column label="包间号" align="center" prop="compartmentNubmer" />
      <el-table-column
        label="包间名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="compartmentName"
      />
     
      <el-table-column label="收费方式" align="center" prop="tollCollectionManner" >
         <template slot-scope="scope">
          <span>{{scope.row.tollCollectionManner == 1 ? "按床位" : "按包间"}}</span>
        </template>
      </el-table-column>

      

      <el-table-column label="床位数量" align="center" prop="bedNumber" />
      <el-table-column label="床位价格" align="center" prop="price" />
      <el-table-column label="床位提成" align="center" prop="royalty" />

      <el-table-column
        label="包间价格"
        align="center"
        prop="privateRoomPrice"
      />
      <el-table-column
        label="包间提成"
        align="center"
        prop="privateRoomRoyalty"
      />
     
      

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span  :style="{ color: scope.row.status ? '#808080' : '#FF5733' }">{{scope.row.status == "1" ? "上架" : "下架"}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="200"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
      />

      <el-table-column
        label="修改人"
        align="center"
        prop="updateBy"
        width="200"
      />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="200"
      />



      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
        width="250"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:booth:edit']"
            >修改</el-button
          >
          <!--:disabled="scope.row.status === 1"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handleGround(scope.row, (status = 1))"
             v-hasPermi="['business:booth:grounding']"
            :disabled="scope.row.status === 1"
            >上架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleGround(scope.row, (status = 2))"
             v-hasPermi="['business:booth:undercarriage']"
            :disabled="scope.row.status === 2"
            >下架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:booth:del']"
            :disabled="scope.row.status === 1"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改门店对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="归属区域" prop="regionId">
              <el-select
                clearable
                v-model="form.regionId"
                @change="changeRegion"
              >
                <el-option
                  v-for="(item, index) of region"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属门店" prop="storeId">
              <el-select clearable v-model="form.storeId" @change="changestore">
                <el-option
                  v-for="(item, index) of store"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="收费方式" prop="tollCollectionManner">
          <el-select clearable v-model="form.tollCollectionManner">
            <el-option label="按床位" value="1"></el-option>
            <el-option label="按包间" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="包间名称" prop="compartmentName">
              <el-input
                v-model="form.compartmentName"
                placeholder="请输入包间名称"
                style="width: 220px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="包间号" prop="compartmentNubmer">
              <el-input
                v-model="form.compartmentNubmer"
                placeholder="请输入包间号"
                style="width: 220px"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="form.tollCollectionManner == 2">
            <el-form-item label="包间价格" prop="privateRoomPrice">
              <el-input
                v-model="form.privateRoomPrice"
                placeholder="请输入包间价格"
                style="width: 220px"
              >
                <template slot="append">张</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.tollCollectionManner == 2">
            <el-form-item label="包间提成" prop="privateRoomRoyalty">
              <el-input
                v-model="form.privateRoomRoyalty"
                placeholder="请输入包间提成"
                style="width: 220px"
                ><template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="床位数量" prop="bedNumber">
              <el-input
                v-model="form.bedNumber"
                placeholder="请输入床位数量"
                style="width: 220px"
              >
                <template slot="append">张</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.tollCollectionManner == 1">
            <el-form-item label="床位价格" prop="price">
              <el-input
                v-model="form.price"
                placeholder="请输入床位价格"
                style="width: 220px"
                ><template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" v-if="form.tollCollectionManner == 1">
            <el-form-item label="床位提成" prop="royalty">
              <el-input
                v-model="form.royalty"
                placeholder="请输入床位提成"
                style="width: 220px"
                ><template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select clearable v-model="form.status">
                <el-option
                  v-for="(item, index) of List"
                  :key="index"
                  :label="item.value"
                  :value="item.key"
                  >{{ item.value }}</el-option
                >
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStore,
  getStore,
  delStore,
  addStore,
  updateStore,
  listMemberRegion,
  listMemberStore,
} from "@/api/system/booth";

export default {
  name: "Store",
  data() {
    return {
      map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      nameList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compartmentName: null,
        storeName: null,
        bedNumber: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeName: [
          { required: true, message: "请输入门店名称", trigger: "blur" },
        ],
        compartmentName: [
          { required: true, message: "请输入包间名称", trigger: "blur" },
        ],
        compartmentNubmer: [
          { required: true, message: "请输入包间号", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur" },
        ],
        bedNumber: [
          { required: true, message: "请输入床位数量", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur" },
        ],
        price: [
          { required: true, message: "请输入包间号", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur" },
        ],
        openingTime: [
          { required: true, message: "请选择营业时间", trigger: "blur" },
        ],
        tollCollectionManner: [
          { required: true, message: "请选择收费方式", trigger: "blur" },
        ],
        isInitiate: [
          { required: true, message: "请选择是否营业", trigger: "blur" },
        ],
        addressAll: [
          { required: true, message: "请选择省市区", trigger: "blur" },
        ],
        regionId: [
          { required: true, message: "请选择所属区域", trigger: "blur" },
        ],

        privateRoomPrice: [
          { required: true, message: "包间价格", trigger: "blur" },
          { pattern: /^\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],
        privateRoomRoyalty: [
          { required: true, message: "包间提成", trigger: "blur" },
          { pattern: /^\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],

        royalty: [
          { required: true, message: "请输入床位提成", trigger: "blur" },
          { pattern: /^\d+(\.\d+)?$/, message: "请输入数字", trigger: "blur" },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
      },
      options: [],
      List: [
        {
          key: 1,
          value: "上架",
        },
        {
          key: 2,
          value: "下架",
        },
      ],
      region: [],
      store: [],
    };
  },
  created() {
    this.getList();
    this.listregion();
    listMemberStore().then((res) => {
      this.store = res.data.map((rItem) => {
        return {
          key: rItem.id,
          value: rItem.storeName,
        };
      });
    });
  },
  methods: {
    listregion() {
      listMemberRegion().then((res) => {
        this.region = res.data.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.regionName,
          };
        });
      });
    },
    changeRegion(value) {
      console.log(value, "区域");
      // const regionStr = value.length > 1 ? value.join(',') : value[0]
      // this.form.regionId = value[0]
      const selectedOption = this.region.find((item) => item.key === value);
      const selectedKey = selectedOption.key;
      const selectedValue = selectedOption.value;
      console.log(`Selected Key: ${selectedKey}`);
      console.log(`Selected Value: ${selectedValue}`);
      this.form.regionName = selectedValue;
      listMemberStore({ regionId: value }).then((res) => {
        this.store = res.data.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.storeName,
          };
        });
      });
      // this.form.regionName=selectedValue
    },
    changestore(value) {
      console.log(value, "门店");
      // let arr = []
      // value.map((val) => {
      //   console.log(this.store.find(item => item.key === val))
      //   arr.push(this.store.find(item => item.key === val).value)
      //   return val
      // })
      const selectedOption = this.store.find((item) => item.key === value);
      const selectedKey = selectedOption.key;
      const selectedValue = selectedOption.value;
      console.log(`Selected Key: ${selectedKey}`);
      console.log(`Selected Value: ${selectedValue}`);
      this.form.storeName = selectedValue;
      // this.form.storeId = value[0]
      // this.form.storeName = arr.join(',')
    },
    /** 查询门店列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then((response) => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.nameList = selection.map((item) => item.compartmentName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加包间";
    },
    // 上下架
    handleGround(row, status) {
      const params = {
        id: row.id,
        status: status,
      };
      this.$confirm(
        `此操作将${status === 1 ? "上架" : "下架"}该数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updateStore(params).then((res) => {
            this.$message({
              type: "success",
              message: `${status === 1 ? "上架" : "下架"}成功!`,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消上下架",
          });
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getStore(id).then((response) => {
        this.form = response.data;
        this.form.tollCollectionManner =
          response.data.tollCollectionManner + "";
        this.open = true;
        this.title = "修改包间";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log(this.form);
        // this.form.regionIds = this.form.regionIds.length > 1 ? this.form.regionIds.join(',') : this.form.regionIds[0]
        // this.form.storeIds = this.form.storeIds.length > 1 ? this.form.storeIds.join(',') : this.form.storeIds[0]
        if (valid) {
          if (this.form.id != null) {
            updateStore(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addStore(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const name = row.compartmentName || this.nameList;
      this.$modal
        .confirm('是否确认删除包间为"' + name + '"的数据项？')
        .then(function () {
          return delStore(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/store/export",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
